import urllib2
import uuid

class SIF_RefId():
    """SIF RefId
    SIF_RefId() generates a UUID, a universally unique identifier,
    via RFC 4122 method 1, to associate with data and other objects.
    Once associated with an object, the same RefId should ALWAYS
    refer to that object, for longitudinal consistency.
    A SIF_RefId is a string of 32 hexadecimal characters,
    letters A-F always in upper case. No punctuation between characters.
    """
    def __init__(self):
        self.id = uuid.uuid1().hex.upper()
    def __str__(self):
        return self.id

class error():
    def __init__(self, category, code):
        self.category = category
        self.code = code

class send:
    def __init__(self, message):
        message.SIFMsgId = SIF_RefId()
        message.store()
    def transmit:
        message.transmit
    def ack:
        msg_store.retrieve(RefId).remove

def handle_request(SIF_Request):
    """Handle all requests, even for objects we don't support.
    """
    if duplicate(SIF_Request.RefId):
        msg_reply(status=duplicate)
    if unsupported_object(SIF_Request):
        send(msg(SIF_Response, SIF_Error('invalid object'), SIF_PacketNumber = 1,
             SIF_MorePackets = no_further_packets))
    check_versions(SIF_Request)
    data_set = SIF_Request.SIF_Query.process
    # may support SIF_ExtendedQuery, which must be provisioned
    packets = SIF_Response(construct_xml_stream(data_set), SIF_MaxBufferSize)
    if len(packets) > 1:
        SIF_Response(SIF_Error, SIF_MorePackets = ...?)
    if any_other_error:
        SIF_Response(SIF_Error, SIF_MorePackets = no_more_packets)

class msg():
    """SIF agent message handler
    Handle all messages, including messages and objects we don't support.
    """
    def __init__(self, SIF_Message):
        validate(SIF_Message) # use XML schema
        if invalid:
            msg_return(error)
        if unsupported(SIF_Message):
            msg_return(SIF_Ack, error('Generic Message Handling', "Message not supported"))
        reply(SIF_Message, SIF_Ack, ...)
        if message_type(SIF_Message) == SIF_Request:
            handle_request(SIF_Message.request)

class zisComm():
    """SIF HTTPS, optionally SIF HTTP communication with ZIS
    Other protocols may be supported. SIF HTTPS is required.
    """
    def __init__(self, request_handler=none):
        # Set up listener if request_handler.
    def register(self, settings):
        """We must register with a ZIS to communicate,
        and may change or retransmit registration settings."""
        self.msg(SIF_Register, info)
    def support(self, data_and_messages):
        self.msg(SIF_Provision, info)
        self.msg(SIF_Provide, info)
        self.msg(SIF_Subscribe, info)
    def publish(self, event):
        self.event(event)
        # Do not send unsupported or unchanged optional fields, even empty.
        # For Add events, include all mandatory fields.
    def status(self):
        SIF_ZoneStatus
    def send(self, message):
        # POST
        # utf-8 only
        # only HTTP reply is 200-OK
